package com.jfok.server.logic;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jfok.server.DAO.SystemBaseDAO;
import com.jfok.server.hibernate.addition._Addition;
import com.jfok.server.service.SystemGlobalInfoService;
import com.jfok.shared.module.grid.GridFilterData;

@Service
public class _AdditionLogic extends BaseOperateLogic<_Addition> {

	@Resource
	private SystemBaseDAO systemBaseDAO;

	
	public Integer getAdditionCount(String moduleId, String keyValue) {
		Session session = systemBaseDAO.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(_Addition.class);
		criteria.add(Restrictions.eq(_Addition.MODULEID, moduleId));
		criteria.add(Restrictions.eq(_Addition.MODULEKEYID, Integer.parseInt(keyValue)));
		criteria.setProjection(Projections.count(_Addition.MODULEKEYID));
		Integer maxOrder = ((Long) criteria.uniqueResult()).intValue();
		session.close();
		return maxOrder == null ? 0 : maxOrder;
	}

	
	public int changeAdditionIdkey(String moduleId, String oldkey, String newkey) {
		if (getAdditionCount(moduleId, oldkey) > 0) {
			Session session = systemBaseDAO.getSessionFactory().getCurrentSession();
			Query query1 = session.createSQLQuery("update _Addition set tf_moduleIdvalue = :newkey "
					+ " where tf_moduleId=:mid and tf_moduleIdvalue=:oldkey ");
			query1.setParameter("newkey", Integer.parseInt(newkey));
			query1.setParameter("mid", moduleId);
			query1.setParameter("oldkey", Integer.parseInt(oldkey));
			int count = query1.executeUpdate();
			// session.close();
			return count;
		} else
			return 0;
	}

	@Override
	public Map<String, Object> getNewDefultValue(HttpServletRequest request,
			GridFilterData gridFilterData) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("tf_moduleId", gridFilterData.getParentModuleFilter().getModuleId());
		result.put("tf_moduleIdvalue", gridFilterData.getParentModuleFilter().getEqualsValue());
		// System.out.print(gridFilterData);
		String moduleId = gridFilterData.getParentModuleFilter().getModuleId();
		String keyValue = gridFilterData.getParentModuleFilter().getEqualsValue();
		Session session = systemBaseDAO.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(_Addition.class);
		criteria.add(Restrictions.eq(_Addition.MODULEID, moduleId));
		criteria.add(Restrictions.eq(_Addition.MODULEKEYID, Integer.parseInt(keyValue)));
		criteria.setProjection(Projections.max(_Addition.ADDITIONORDER));
		Integer maxOrder = (Integer) criteria.uniqueResult();
		if (maxOrder != null) {
			result.put(_Addition.ADDITIONORDER, maxOrder + 1);
			criteria = session.createCriteria(_Addition.class);
			criteria.add(Restrictions.eq(_Addition.MODULEID, moduleId));
			criteria.add(Restrictions.eq(_Addition.MODULEKEYID, Integer.parseInt(keyValue)));
			criteria.add(Restrictions.eq(_Addition.ADDITIONORDER, maxOrder));
			_Addition addition = (_Addition) criteria.list().get(0);
			String n = addition.getTf_name();
			int pos = n.indexOf("◆");
			if (pos != -1)
				n = n.substring(0, pos);
			result.put(_Addition.ADDITIONNAME, n + "◆" + (maxOrder + 1));
			result.put("_t9502___tf_additionTypeId", ""
					+ addition.getTf_AdditionType().getTf_additionTypeId());

			if (addition.getTf_AdditionFileType() != null)
				result.put("_t9503___tf_additionFileTypeId", ""
						+ addition.getTf_AdditionFileType().getTf_additionFileTypeId());

		} else {
			result.put(_Addition.ADDITIONORDER, 1);
		}
		session.close();

		if (SystemGlobalInfoService.getSysteminfo().getTf_AdditionReduceMode() != null)
			result
					.put("_t9504___tf_reduceModeId", ""
							+ SystemGlobalInfoService.getSysteminfo().getTf_AdditionReduceMode()
									.getTf_reduceModeId());
		return result;
	}
}
